package com.orion;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.orion.entity.Building;
import com.orion.entity.Car;
import com.orion.entity.Comment;
import com.orion.entity.FamousPerson;
import com.orion.service.rhlc.IRhlcEsService;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.Avg;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author Administrator
 * @date 2022/3/4
 */
@SpringBootTest
public class JunitTestForRHLC {

    @Resource(name = "rhlcEsService")
    private IRhlcEsService rhlcEsService;

    @Resource(name = "objectMapper")
    private ObjectMapper objectMapper;

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Test
    public void createIndex() throws IOException {
        CreateIndexResponse fake_car = rhlcEsService.createIndex("fake_car", 1, 2);
        System.out.println(fake_car);
    }

    @Test
    public void indexInfo() throws IOException {
        GetIndexResponse response = rhlcEsService.indexInfo("fake_car");
        System.out.println(response);
    }

    @Test
    public void deleteIndex() throws IOException {
        AcknowledgedResponse acknowledgedResponse = rhlcEsService.deleteIndex("fake_car");
        System.out.println(acknowledgedResponse);
    }

    /**
     * 多次添加覆盖，version增加
     * @throws IOException
     */
    @Test
    public void addDoc() throws IOException {
        FamousPerson famousPerson = new FamousPerson();
        famousPerson.setAge(66);
        famousPerson.setSex("女");
        famousPerson.setPName("宋庆龄");

        Building building = new Building();
        building.setId(21L);
        building.setCreateTime(LocalDate.now());
        building.setHistoryYears(75);
        building.setName("重庆三峡");
        building.setShortname("三峡");
        building.setFamousPerson(famousPerson);

        IndexResponse response = rhlcEsService.addDoc("building", building);
        System.out.println(response);
    }

    @Test
    public void queryDocById() throws IOException {
        GetResponse response = rhlcEsService.queryDocById("building", 2L);
        Building building = objectMapper.readValue(response.getSourceAsBytes(), Building.class);
        building.setId(Long.valueOf(response.getId()));
        System.out.println(building);
    }

    @Test
    public void update() throws IOException {
        FamousPerson famousPerson = new FamousPerson();
        famousPerson.setAge(12);
        famousPerson.setSex("女");
        famousPerson.setPName("嘻嘻");

        Building building = new Building();
        building.setId(21L);
        building.setCreateTime(LocalDate.now().minusDays(20));
        building.setHistoryYears(75);
        building.setShortname("普陀山");
        building.setFamousPerson(famousPerson);

        UpdateResponse response = rhlcEsService.update("building", building);
        System.out.println(response);
    }

    @Test
    public void delete() throws IOException {
        DeleteResponse deleteResponse = rhlcEsService.delete("building", 21L);
        System.out.println(deleteResponse);
    }

    @Test
    public void queryDeepPage() throws IOException {
        Building building = new Building();
        building.setName("广东");
        building.setHistoryYears(17);
        building.setCreateTime(LocalDate.parse("2007-06-01", dtf));

        SearchResponse response = rhlcEsService.queryDeepPage("building", 20, 0,building);
        System.out.println(response);
    }

    @Test
    public void queryComposite() throws IOException {
        Building building = new Building();
        building.setName("广东");
        building.setHistoryYears(4);
        building.setCreateTime(LocalDate.parse("2007-01-01", dtf));
        building.setShortname("滕王阁");

        SearchResponse response = rhlcEsService.queryComposite("building", building);
        SearchHits hits = response.getHits();
        hits.forEach(item -> {
            System.out.println(item.toString() + "--" + item.getId());
        });
    }

    @Test
    public void aggs() throws IOException {
        SearchResponse response = rhlcEsService.aggs("cars");
        Aggregations aggregations = response.getAggregations();
        aggregations.forEach(item -> {
            System.out.println("key :" + item.getName());
            Terms terms = (Terms) item;
            List<? extends Terms.Bucket> buckets = terms.getBuckets();
            for (Terms.Bucket bucket : buckets) {
                System.out.println("make : " + bucket.getKey().toString() +" , docCount : " + bucket.getDocCount());
            }
        });
    }

    @Test
    public void aggsComposite() throws IOException {
        SearchResponse response = rhlcEsService.aggsComposite("cars");
        Aggregations aggregations = response.getAggregations();
        aggregations.forEach(item -> {
            System.out.println("key : " + item.getName());
            Terms terms = (Terms) item;
            List<? extends Terms.Bucket> buckets = terms.getBuckets();
            buckets.forEach(subItem -> {
                System.out.println(subItem.getKey() + "--" + subItem.getDocCount());
                Aggregations subItemAggregations = subItem.getAggregations();

                subItemAggregations.forEach(lastItem -> {
                    Avg avg = (Avg) lastItem;
                    System.out.println("key : " + lastItem.getName() + "--- value : " + avg.getValue());
                    System.out.println("############################");
                });
            });
            System.out.println("---------------------------------");
        });
    }

    @Test
    public void aggsMultiGroup() throws IOException {
        SearchResponse response = rhlcEsService.aggsMultiGroup("cars");
        System.out.println(response);
    }


    @Test
    public void aggsBros() throws IOException {
       SearchResponse response = rhlcEsService.aggsBros("cars");
       System.out .println(response);
    }

    @Test
    public void complexAggs() throws IOException {
        SearchResponse response = rhlcEsService.complexAggs("cars");
        System.out .println(response);
    }

    @Test
    public void aggsFilterQuery() throws IOException {
        Car car = new Car();
        car.setSold(LocalDate.parse("2014-11-01",dtf));
        car.setMake("honda");
        SearchResponse response = rhlcEsService.aggsFilterQuery("cars",car);
        System.out .println(response);
    }

    @Test
    public void suggest() throws IOException {
        SearchResponse response = rhlcEsService.suggest("my_suggester","天");
        System.out.println(response);
    }

    @Test
    public void nested() throws IOException {
        Comment comment = new Comment();
        comment.setName("alice");
        comment.setAge((short) 31);
        SearchResponse response = rhlcEsService.nested("nested_test", comment);
        System.out.println(response);
    }
}
